/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DBLayer;

import DBLayer.Interfaces.IContainer;
import ModelLayer.OrderLine;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hedevilla
 */
public class DBOrderLine extends DatabaseAccess implements IContainer<OrderLine>
{
    public DBOrderLine()
    {
        super();
    }

    public OrderLine getObject(int OrderID)
    {
        throw new UnsupportedOperationException("Not needed");
    }

    public ArrayList<OrderLine> getObjects()
    {
        throw new UnsupportedOperationException("Not needed");
    }

    public ArrayList<OrderLine> getOrderLinesByOrderID(int OrderID)
    {
        String query = "Select OrderID, ProductID, Quantity From [OrderLine] Where OrderID = " + OrderID;
        ResultSet result = ExecuteQuery(query);
        GenericModelBinder<OrderLine> modelBinder = new GenericModelBinder<OrderLine>(OrderLine.class);

        try
        {
            ArrayList<OrderLine> orderLines = modelBinder.BindModels(result);
            for (OrderLine orderLine : orderLines)
            {
                attachModels(orderLine);
            }
            return orderLines;
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DBEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            Logger.getLogger(DBEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            Logger.getLogger(DBEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;

    }
    DBProduct dbProduct;
    DBOrder dbOrder;

    public OrderLine attachModels(OrderLine orderLine)
    {
        if (dbProduct == null)
        {
            dbProduct = new DBProduct();
        }
        if (dbOrder == null)
        {
            dbOrder = new DBOrder();
        }

        int productID = orderLine.getProductID();
        orderLine.setProduct(dbProduct.getObject(productID));
        int orderID = orderLine.getOrderID();
        orderLine.setOrder(dbOrder.getObject(orderID));

        return orderLine;
    }

    public boolean SaveObject(OrderLine object)
    {
        int affectedRows = 0;

        String sql = String.format("INSERT INTO OrderLine Values (%1$s, %2$s, %3$s)",
                object.getOrder().getOrderID(), 
                object.getProduct().getProductID(),
                object.getQuantity());
        affectedRows = ExecuteNoResultQuery(sql);
        return affectedRows > 0;
    }

    public boolean DeleteObject(OrderLine object)
    {
        int affectedRows = 0;

        String sql = String.format("DELETE FROM OrderLine WHERE OrderID = %1$ and ProductID = %2$",
                object.getOrder().getOrderID(),
                object.getProduct().getProductID());
        affectedRows = ExecuteNoResultQuery(sql);
        return affectedRows > 0;
    }

    public boolean UpdateObject(OrderLine object)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
